﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Leuphana.GuidedTour.App.ViewModels;
using System.Device.Location;

namespace Leuphana.GuidedTour.App.Pages
{
    public partial class HotSpotPage : PhoneApplicationPage
    {
        private HotSpotViewModel _viewModel;

        private GeoCoordinateWatcher _watcher;

        public HotSpotPage()
        {
            InitializeComponent();

            if (App.UsePosition)
            {
                // Create geowatcher
                _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                _watcher.PositionChanged += _watcher_PositionChanged;
                _watcher.StatusChanged += _watcher_StatusChanged;

                _watcher.MovementThreshold = 15;
            }
        }

        void _watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {

        }

        void _watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                LocationPin.Visibility = Visibility.Visible;
                LocationPin.Location = e.Position.Location;
            });
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Page is new, get HotSpot ID from query string
            if (e.NavigationMode == NavigationMode.New)
            {
                string id;

                if (NavigationContext.QueryString.TryGetValue("id", out id))
                {
                    // Get id from query string
                    id = NavigationContext.QueryString["id"] as String;

                    // Create new view model
                    _viewModel = App.GetHotSpot(Int32.Parse(id));
                }
                else
                {
                    // Create new view model
                    _viewModel = App.GetTour().HotSpots.First();
                }

                // Start data loading
                _viewModel.LoadData();
            }

            // Page was tombstoned, get HotSpot ID from state
            else if (_viewModel == null)
            {
                object id;
                if (State.TryGetValue("id", out id))
                {
                    _viewModel = App.GetHotSpot((int)id);
                    _viewModel.LoadData();
                }
            }

            // Reset view
            _viewModel.SelectedArticle = null;

            // Update datacontext
            DataContext = _viewModel;

            base.OnNavigatedTo(e);
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            // Save HotSpot ID to state
            State["id"] = _viewModel.ID;
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (_viewModel.SelectedArticle != null)
            {
                // Just close overlay and cancel back navigation
                _viewModel.SelectedArticle = null;
                e.Cancel = true;
            }

            base.OnBackKeyPress(e);
        }

        private void Button_Activate_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            var id = (int)button.CommandParameter;

            _viewModel.SelectedArticle = _viewModel.Articles.FirstOrDefault(a => a.ID == id);
        }
        private void Button_Deactivate_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.SelectedArticle = null;
        }

        private void AppBar_Prev_Click(object sender, EventArgs e)
        {
            if (_viewModel.HasPrev)
            {
                DataContext = _viewModel = _viewModel.PrevItem;
                _viewModel.LoadData();
            }
        }
        private void AppBar_Next_Click(object sender, EventArgs e)
        {
            if (_viewModel.HasNext)
            {
                DataContext = _viewModel = _viewModel.NextItem;
                _viewModel.LoadData();
            }
        }

        private void AppBar_Map_Click(object sender, EventArgs e)
        {
            if (MapPanel.Visibility == Visibility.Collapsed)
            {
                OpenMapPanelStoryboard.Begin();
                Map.SetView(_viewModel.Location, 14);

                if (_watcher != null)
                {
                    _watcher.Start();
                }
            }
            else
            {
                _watcher.Stop();
                CloseMapPanelStoryboard.Begin();
            }
        }
    }
}